/**
 * Copyright &copy; 2015-2020 <a href="www.dsesa.cn/">ESA</a> All rights reserved.
 */
package com.jeeplus.modules.sys.web;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.sys.entity.ArchCostItem;
import com.jeeplus.modules.sys.entity.DictValue;
import com.jeeplus.modules.sys.service.ArchCostItemService;
import com.jeeplus.modules.sys.service.DictTypeService;
import com.jeeplus.modules.sys.service.DictValueService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xujin
 * @version 2019-10-08
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/archCostItem")
public class ArchCostItemController extends BaseController {

	@Autowired
	private ArchCostItemService archivescostitemService;

	@Autowired
	private DictValueService dictValueService;

	@Autowired
	private DictTypeService dictTypeService;
	
	@RequestMapping(value = {"list", ""})
	public String list() {
		return "modules/sys/archCostItem/archCostItemList";
	}
	
	@ResponseBody
	@RequestMapping(value = "getArchCostItem")
	public Map<String, Object> getArchCostItem(@RequestParam(value = "dictValueId") String dictValueId) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(dictValueId == null || "".equals(dictValueId)){
			map.put("rows","[]");
			map.put("total",0);
		}else{
			List<ArchCostItem> list = archivescostitemService.getCostList(dictValueId);
			map.put("rows",list);
			map.put("total", list.size());
		}
		return map;
	}
	
	@ResponseBody
	@RequestMapping(value = "data")
	public Map<String, Object> data(DictValue dictValue, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<DictValue> page = dictValueService.findPage(new Page<DictValue>(request, response), dictValue);
		return getBootstrapData(page);
	}

	@ResponseBody
	@RequestMapping(value = "costList")
	public Map<String, Object> costList(DictValue dictValue, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DictValue> page = dictValueService.findCostPage(new Page<DictValue>(request, response), dictValue);
		return getBootstrapData(page);
	}

	@RequestMapping(value = "archCostItemForm")
	public String archCostItemForm(String archCostItemId, String dictValueId, Model model) {
		List<DictValue> dictValueList = new ArrayList<>();
		if(archCostItemId == null || "".equals(archCostItemId)){
			dictValueList = dictTypeService.get("d82903f750e840fca09e7f54fadada47").getDictValueList();
		}else{
			DictValue dictValue = new DictValue();
			ArchCostItem archCostItem = dictValueService.getArchCostItem(dictValueId);
			dictValue.setId(archCostItem.getCostitemId());
			dictValue.setLabel(archCostItem.getCostitemName());
			dictValueList.add(dictValue);
		}
		model.addAttribute("dictValueList", dictValueList);
		return "modules/sys/archCostItem/archCostItemForm";
	}

	@RequestMapping(value = "costSelect")
	public String costSelect(boolean isMultiSelect, Model model) {
		model.addAttribute("isMultiSelect", isMultiSelect);
		return "modules/sys/archCostItem/costSelect";
	}

	/**
	 * 款项分配
	 */
	@ResponseBody
	@RequestMapping(value = "assignCost")
	public AjaxJson assignCost(String dictValueId, String []ids) {
		AjaxJson j = new AjaxJson();
		StringBuilder msg = new StringBuilder();
		int newNum = ids.length;
		boolean b = archivescostitemService.setCost(dictValueId, ids);
		if(b){
			j.setSuccess(true);
			j.setMsg("已成功分配 "+newNum+" 个款项");
		}else {
			j.setSuccess(false);
			j.setMsg("添加失败");
		}
		return j;
	}

	@ResponseBody
	@RequestMapping(value = "deleteCost")
	public AjaxJson deleteCost(String id) {
		AjaxJson j = new AjaxJson();
		archivescostitemService.deleteDictValue(id);
		j.setSuccess(true);
		j.setMsg("删除键值成功！");
		return j;
	}

	/**
	 * 修改款项状态
	 */
	@ResponseBody
	@RequestMapping(value = "changeStatus")
	public AjaxJson changeStatus(@RequestParam(value = "jsonStr") String jsonStr) {
		AjaxJson j = new AjaxJson();
		jsonStr = jsonStr.replaceAll("&quot;","\"");
		JSONObject jsonObject = new JSONObject().fromObject(jsonStr);
		ArchCostItem archCostItem = (ArchCostItem) jsonObject.toBean(jsonObject,ArchCostItem.class);

		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(archCostItem);
		if (StringUtils.isNotBlank(errMsg)){
			j.setSuccess(false);
			j.setMsg(errMsg);
			return j;
		}
		archivescostitemService.save(archCostItem);
		j.setSuccess(true);
		j.setMsg("修改成功！");
		return j;
	}

}
